package com.etooling.pdm.controller;

import com.etooling.framework.svo.ReplyObj;
import com.etooling.pdm.service.GoodsSpecsStockRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@RequestMapping(value = "/admin/pdm/goodsSpecsStock")
@Controller
@EnableAutoConfiguration
public class GoodsSpecsStockRecordController {
    @Autowired
    private GoodsSpecsStockRecordService goodsSpecsStockRecordService;

    /**
     * 操作库存
     *
     * @param goodsSpecsId  物料ID
     * @param stockNum      数量
     * @param operationType 操作类型：IN-入库，OUT-出库
     * @param stockType     库存类型; 例如: 领料, 入库, 出库, 调拨 等
     * @param memo          备注
     * @return 操作结果
     */
    @RequestMapping("operateStock")
    @ResponseBody
    public ReplyObj operateStock(
            HttpServletRequest request,
            @RequestParam String goodsSpecsId,
            @RequestParam Integer stockNum,
            @RequestParam String operationType,
            @RequestParam String stockType,
            @RequestParam(required = false) String memo) {
        return goodsSpecsStockRecordService.operateStock(request, goodsSpecsId, stockNum, operationType, stockType, memo);
    }

    /**
     * 撤销库存记录
     *
     * @param recordId     记录ID
     * @param revokeReason 撤销原因
     * @return 撤销结果
     */
    @RequestMapping("revokeStockRecord")
    @ResponseBody
    public ReplyObj revokeStockRecord(HttpServletRequest request,
                                      @RequestParam String recordId,
                                      @RequestParam String revokeReason) {
        return goodsSpecsStockRecordService.revokeStockRecord(request, recordId, revokeReason);
    }

    /**
     * 分页查询库存记录
     *
     * @param request
     * @param isAll         是否查询所有记录，true-是，false-否
     * @param operationType 操作类型，例如：IN-入库，OUT-出库
     * @param goodsSpecsId  物料ID
     * @param status        记录状态，例如：NORMAL-正常，REVOKED-已撤回
     * @param orderBy       排序字段，例如：recordTime-记录时间
     * @param orderType     排序方式，true-升序，false-降序
     * @param pageNum       页码
     * @param pageSize      每页记录数
     * @return
     */
    @RequestMapping("pageQueryStockRecords")
    @ResponseBody
    public ReplyObj pageQueryStockRecords(
            HttpServletRequest request,
            @RequestParam(required = false, defaultValue = "false", value = "isAll") Boolean isAll,
            @RequestParam(required = false, defaultValue = "", value = "operationType") String operationType,
            @RequestParam(required = false, defaultValue = "", value = "goodsSpecsId") String goodsSpecsId,
            @RequestParam(required = false, defaultValue = "", value = "status") String status,
            @RequestParam(required = false, defaultValue = "", value = "orderBy") String orderBy,
            @RequestParam(required = false, defaultValue = "", value = "orderType") Boolean orderType,
            @RequestParam(defaultValue = "1", required = false, value = "pageNum") Integer pageNum,
            @RequestParam(defaultValue = "10", required = false, value = "pageSize") Integer pageSize) {
        return goodsSpecsStockRecordService.pageQueryStockRecords(request, isAll, operationType, goodsSpecsId, status, orderBy, orderType, pageNum, pageSize);
    }
}
